//
//  NSOperationController.m
//  GCD
//
//  Created by 宜必鑫科技 on 2017/11/6.
//  Copyright © 2017年 宜必鑫科技. All rights reserved.
//

#import "NSOperationController.h"
#import <Masonry.h>
#import <ReactiveObjC.h>
#import "WJOperation.h"

@interface NSOperationController ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImage *image1;
@property (nonatomic, strong) UIImage *image2;
@end

@implementation NSOperationController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
  
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(50);
    }];
    [button setTitle:@"NSInvocationOperation" forState:0];
    button.backgroundColor = [UIColor darkGrayColor];
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self NSInvocationOperation];
    }];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button1];
    button1.backgroundColor = [UIColor darkGrayColor];
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(button.mas_bottom).offset(10);
        make.height.mas_equalTo(50);
    }];
    [button1 setTitle:@"NSBlockOperation" forState:0];
    [[button1 rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self NSBlockOperation];
    }];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button2];
    button2.backgroundColor = [UIColor darkGrayColor];
    [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(button1.mas_bottom).offset(10);
        make.height.mas_equalTo(50);
    }];
    [button2 setTitle:@"NSOperationQueue 第一种" forState:0];
    [[button2 rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self customOperation];
    }];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button3];
    button3.backgroundColor = [UIColor darkGrayColor];
    [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(button2.mas_bottom).offset(10);
        make.height.mas_equalTo(50);
    }];
    [button3 setTitle:@"NSOperationQueue 第二种" forState:0];
    [[button3 rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self block];
    }];
    
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button4];
    button4.backgroundColor = [UIColor darkGrayColor];
    [button4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(button3.mas_bottom).offset(10);
        make.height.mas_equalTo(50);
    }];
    [button4 setTitle:@"NSOperationQueue 第三种" forState:0];
    [[button4 rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self three];
    }];
    
    
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button5];
    button5.backgroundColor = [UIColor darkGrayColor];
    [button5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(button4.mas_bottom).offset(10);
        make.height.mas_equalTo(50);
    }];
    [button5 setTitle:@"WJOperation" forState:0];
    [[button5 rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        WJOperation *operation = [[WJOperation alloc]init];
        [operation start];
    }];
    
    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button6];
    button6.backgroundColor = [UIColor darkGrayColor];
    [button6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(button5.mas_bottom).offset(10);
        make.height.mas_equalTo(50);
    }];
    [button6 setTitle:@"综合练习" forState:0];
    [[button6 rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self download2];
    }];
    
    
    _imageView = [[UIImageView alloc]init];
    [self.view addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.equalTo(button6.mas_bottom).offset(10);
        make.height.mas_equalTo(200);
    }];
}


- (void)NSInvocationOperation
{
    //  01 NSInvocationOperation
    //    01 NSOperation本身是抽象类，只能只有它的子类
    //    02 三个子类分别是：NSBlockOperation、NSInvocationOperation以及自定义继承自NSOperation的类
    //    03 NSOperation和NSOperationQueue结合使用实现多线程并发
    
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImage) object:@""];
    [invocation start];
}

- (void)NSBlockOperation
{
    //NSBlockOperation
    NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"在主线程中执行 %@",[NSThread currentThread]);
    }];
    
    //2.追加操作，追加的操作在子线程中执行
    [block addExecutionBlock:^{
        NSLog(@"---download1--%@",[NSThread currentThread]);
    }];
    
    [block addExecutionBlock:^{
        NSLog(@"---download2--%@",[NSThread currentThread]);
    }];
    
    [block start];
}

// 两种写法 第一种 NSInvocationOperation
- (void)customOperation
{
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImage) object:@""];
    NSInvocationOperation *invocation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImage) object:@""];
    NSInvocationOperation *invocation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImage) object:@""];
    [queue addOperation:invocation];
    [queue addOperation:invocation1];
    [queue addOperation:invocation2];
}

- (void)block
{
    //1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    //2.封装操作
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1----%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"2----%@",[NSThread currentThread]);
        
    }];
    
    [op2 addExecutionBlock:^{
        NSLog(@"3----%@",[NSThread currentThread]);
    }];
    
    [op2 addExecutionBlock:^{
        NSLog(@"4----%@",[NSThread currentThread]);
    }];
    
    //3.添加操作到队列中
    [queue addOperation:op1];
    [queue addOperation:op2];
}

- (void)three
{
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperationWithBlock:^{
         NSLog(@"1----%@",[NSThread currentThread]);
    }];
    [queue addOperationWithBlock:^{
        NSLog(@"2----%@",[NSThread currentThread]);
    }];
    [queue addOperationWithBlock:^{
        NSLog(@"3----%@",[NSThread currentThread]);
    }];
}



- (void)download2
{
    NSLog(@"----");
    //1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    //2.封装操作下载图片1
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSURL *url = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/zhidao/pic/item/6a63f6246b600c3320b14bb3184c510fd8f9a185.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        //拿到图片数据
        self.image1 = [UIImage imageWithData:data];
    }];
    
    
    //3.封装操作下载图片2
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSURL *url = [NSURL URLWithString:@"http://pic.58pic.com/58pic/13/87/82/27Q58PICYje_1024.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        //拿到图片数据
        self.image2 = [UIImage imageWithData:data];
    }];
    
    //4.合成图片
    NSBlockOperation *combine = [NSBlockOperation blockOperationWithBlock:^{
        
        //4.1 开启图形上下文
        UIGraphicsBeginImageContext(CGSizeMake(200, 200));
        
        //4.2 画image1
        [self.image1 drawInRect:CGRectMake(0, 0, 200, 100)];
        
        //4.3 画image2
        [self.image2 drawInRect:CGRectMake(0, 100, 200, 100)];
        
        //4.4 根据图形上下文拿到图片数据
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        //4.5 关闭图形上下文
        UIGraphicsEndImageContext();
        
        //7.回到主线程刷新UI
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            self.imageView.image = image;
            NSLog(@"刷新UI---%@",[NSThread currentThread]);
        }];
        
    }];
    
    //5.设置操作依赖
    [combine addDependency:op1];
    [combine addDependency:op2];
    
    //6.添加操作到队列中执行
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:combine];
}




- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)downLoadImage
{
    
}
@end
